package com.xz.crawlerday02medinumcase.service.impl;

import com.xz.crawlerday02medinumcase.dao.JobInfoDao;
import com.xz.crawlerday02medinumcase.entity.JobInfo;
import com.xz.crawlerday02medinumcase.service.JobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName selfstudy-crawler
 * @title     JobInfoServiceImpl   
 * @package    com.xz.crawlerday02medinumcase.service.impl  
 * @date   2019/10/11 14:25  
 * @explain
 */
@Service
public class JobInfoServiceImpl implements JobInfoService {

    @Autowired
    private JobInfoDao jobInfoDao;


    /**
     * 保存数据
     */
    public void save(JobInfo jobInfo){
        //先从数据库查询数据,根据发布日期查询和url查询
        JobInfo param = new JobInfo();
        param.setUrl(jobInfo.getUrl());
        param.setTime(jobInfo.getTime());

        List<JobInfo> list = this.findByCondition(param);

        if (list == null || list.size() == 0){
            // 没有查询到数据再执行保存或者更新
            this.jobInfoDao.saveAndFlush(jobInfo);
        }
    }


    /**
     * 根据条件查询
     * @param jobInfo
     * @return
     */
    public List<JobInfo> findByCondition(JobInfo jobInfo){

        Example<JobInfo> example = Example.of(jobInfo);

        List<JobInfo> list = this.jobInfoDao.findAll(example);

        return list;
    }

}
